from fuzzywuzzy import fuzz
import pandas as pd
import os
import __init__
import other
start_time, today_time, _hour_= other.time_start()  # start_time,today_time,today_hour=时间('20200731', '20200820', '16:12')
path_db= os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
path = {"gegu": os.path.join(path_db, "db", "dxw", "gegu", '{}.json'),
        "gegu_zhangfu_bankuai": os.path.join(path_db, "db", "dxw", "gegu", "bankuai", '{}.json'),}

path=path["gegu_zhangfu_bankuai"].format(today_time)
dxw_data=pd.read_csv(path)
dxw_bk_list=dxw_data.板块.drop_duplicates()
# print(dxw_bk_list)
ths_bk_df=pd.read_csv('db_temp/ths_bk/gnbk_{}.csv'.format("gn"))
ths_bk_list=ths_bk_df.Name
#print(ths_bk_list)
list_=[]
for i in dxw_bk_list:
    for i_ths in ths_bk_list:
        score=fuzz.token_sort_ratio(i, i_ths)
        if score>50:
            #print(i, i_ths,score)
            list_.append(i_ths)

test=pd.DataFrame(data=list_,columns=["bk_name"])
print(test)
test.to_csv("db_temp/ans_/{}_ths_bk_list.txt".format(today_time), encoding="utf-8")
gn_df= pd.read_csv("db_temp/ths_bk/筛选数据_{}.txt".format("gn"), encoding="utf-8", index_col=0)
print(gn_df)
_df=pd.merge(gn_df, test, how='inner', on='bk_name')
_df.index=_df.bk_name
_df= _df.rename(columns={'S_ID':'code'})
_df=_df.loc[list_]
_df.to_csv("db_temp/ans_/ths_{}.txt".format(today_time), encoding="utf-8")
# print(_df['S_ID'].value_counts())
print(_df)
rixiaojie=pd.read_csv("db_temp/3.5日小结.txt", encoding="utf-8", index_col=0)

_df_1=pd.merge(_df, rixiaojie, how='inner', on='code')
_df_1.to_csv("db_temp/ans_/1_ths_{}.txt".format(today_time), encoding="utf-8")

